﻿using HtmlAgilityPack;
using NewsCollection.Common.Service;
using NewsCollection.Common.UpdateGUI;
using NewsCollection.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NewsCollection.ListWeb
{
    class NgoisaoVn
    {
        ProcessDb pdb = new ProcessDb();
        UpdateButton updateBtn = new UpdateButton();
        public void Run(ListView lv, Button btnStart, Button btnStop, int manuId,string folder)
        {

            Load load = new Load(lv);
            List<CategoryModel> ListCate = new List<CategoryModel>();

      

            string urlGoc = "http://ngoisao.vn/";

         

            //  var item2= GetHtmlAjax(urlGoc, 5);
         
        
          
              
            //    ListCate = load.LoadCatesNoSub(urlGoc, "//ul[@class='tabs']//li[@class='i-menu']", urlGoc, manuId);
            ListCate = load.LoadCatesWithSub(urlGoc, "//ul[@class='tabs']//li[contains(@class,'i-menu')]", "//ul[@class='tabs']//li[contains(@class,'i-menu')]//ul", urlGoc, manuId, "","//a[contains(@class,'pro')]","//a",null,false);

                int dem = 0;



                for (int i = 1; i < ListCate.Count(); i++)
                {
                    //trang dau
            
                 // lay link dau tien sau do cho duoi chay tu 1-> trang cuoi
              
                   
                      
                   //     p.Url = PageFirstAndLast[0].Url.Replace("-1", "-" + j);
                        // featured news
                    
                            var ListFeaturedNews = load.LoadUpdateNewHomeNews(ListCate[i], "//div[@id='cate-focus']//div[@class='top-newhotc2']", "/h2//a", "", "//a", urlGoc);
                            //        var loadFeatured2 = load.LoadUpdateNewHomeNews(p, "//div[@class='category-featured clearfix']//div[@class='more-topstory']//div[@class='story']", "//p[@class='title']//a", "", "//p[@class='photo']//a", urlGoc, "");
                       
                            //      ListFeaturedNews.AddRange(loadFeatured2.ListNewsDetails);

                            for (int k = 0; k < ListFeaturedNews.Count(); k++)
                            {
                                var item = load.LoadNews(ListFeaturedNews[k], "//div[@class='detail-content']", "//div[@style='padding-top:15px; padding-bottom:15px;']//strong[@style='font-size:13px;']", manuId,0, "//div[@class='detail-content']//img", urlGoc,true);
                                if (item != null)
                                {
                                    dem++;
                                    load.AddItemToListView(item);

                                    item.Hot = "Tin hot";
                                    pdb.SaveImgFeatured(folder, item);

                                }

                                //if (dem == 30)
                                //    break;
                            }
                        
                        // load tin khac phia duoi 
                       
                        var ListDetail = load.LoadUpdateNewHomeNews(ListCate[i], "//div[contains(@class,'cate-item ')]", "//h3", "//div[@class='sapo']", "//h3//a", urlGoc);



                    

                        for (int k = 0; k < ListDetail.Count(); k++)
                        {
                            var item = load.LoadNews(ListDetail[k], "//div[@class='detail-content']", "", manuId, 0,"//div[@class='detail-content']//img",urlGoc,false);
                            if (item != null)
                            {
                                dem++;
                                load.AddItemToListView(item);
                                pdb.SaveImgNormal(folder, item);

                            }

                            //if (dem == 30)
                            //    break;
                        }

                      
                 


                   

                }

            
         
      //      System.Windows.Forms.MessageBox.Show("\nThanh cong!Co " + dem + " tin moi");

            updateBtn.UpdateButtonStartAfterRun(btnStart);
            updateBtn.UpdateButtonStopAfterRun(btnStop);
            //updateBtn.UpdateAfterRun(btnStart,btnStop);
            //if (dem == 0)
            //{
            //    MessageBox.Show("\nKhong co san pham moi!");
            //}
            //else MessageBox.Show("\nCo "+dem + " san pham moi");






        }
    }
}
